import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

import login from './components/login.vue'
import home from './components/home.vue'
import userlist from './components/userlist.vue'
import parkingspacelist from './components/parkingspacelist.vue'
import parkingrecord from './components/parkingrecord.vue'
import recharge from './components/recharge.vue'
const router = new VueRouter({
	mode:'history',
	routes:[{
		path:'/',
		component:home,
		children:[
			{
				path:'/userlist',
				component:userlist,
			},
			{
				path:'/parkingspacelist',
				component:parkingspacelist,
			},
			{
				path:'/parkingrecord',
				component:parkingrecord,
			},
			{
				path:'/recharge',
				component:recharge,
			}
		]
	},{
		path:'/login',
		component:login
	}]
})

router.beforeEach((to,from,next)=>{
	
	if(to.path=='/login')
	{
		sessionStorage.removeItem("user");
		next();
	}
	else
	{
		let user = sessionStorage.getItem("user");
		if(user!=null)
		{
			next();
		}
		else
		{
			next({path:'/login'});
		}
	}
	
})

export default router